package logic

import (
	"context"
	"mall_srv/models"

	"mall_srv/good"
	"mall_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品发布
func (l *GoodsListLogic) GoodsList(in *good.GoodsListRequest) (*good.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	var g models.Goods
	goodList, err := g.List()
	if err != nil {
		logx.Info("商品信息查询失败", err)
		return nil, err
	}
	var goodsList []*good.GoodsList
	for _, goods := range goodList {
		list := good.GoodsList{
			GoodsId:    int64(goods.ID),
			GoodsTitle: goods.GoodsTitle,
			GoodsImage: goods.GoodsImage,
			GoodsPrice: float32(goods.GoodsPrice),
			GoodsStock: goods.GoodsStock,
			GoodStatus: goods.Status,
			Status:     goods.Status,
		}
		goodsList = append(goodsList, &list)
	}
	return &good.GoodsListResponse{
		List: goodsList,
	}, nil
}
